/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package carrello.impl.controller.user;

import carrello.impl.controller.AbstractCTRL;
import carrello.model.association.ProductBelongsToCategory;
import carrello.model.entities.Category;
import carrello.model.entities.Product;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Usman
 */
public class BrowseCategoryCTRL extends AbstractCTRL {

    public static List<Product> getProductByCategoryName(String catName) {
        List<Product> products = new ArrayList<Product> ();
        ProductBelongsToCategory.getProductByCategoryName(catName);
        
        retrieveAllChildren (catName, products);
        return products;
    }
    private static void retrieveAllChildren (String catName, List<Product> products ) {
        List<Product> list = ProductBelongsToCategory.getProductByCategoryName(catName);
        if ( list != null && list.size() > 0)
            products.addAll(list);
        
        Category cat = Category.getCategoryByName(catName);
        if ( cat == null )
            return;
        
        for ( Category c: cat.getchildren() ) {
            retrieveAllChildren ( c.getName(), products);
        }
    }
    
}
